package com.bw.mallshop.mvvm.viewmodel;

import android.text.TextUtils;
import androidx.databinding.ObservableField;
import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.dao.UserSingle;
import com.bw.mallshop.entity.UserEntity;
import com.bw.mallshop.entity.UserInsertEntity;
import com.bw.mallshop.field.EventAction;
import com.bw.mallshop.mvvm.model.LoginModel;
import com.bw.mallshop.mvvm.view.ForgetPWDActivity;
import com.bw.mallshop.mvvm.view.RegisterActivity;
import org.greenrobot.eventbus.EventBus;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginViewModel extends BaseViewModel<LoginModel> {

    public ObservableField<String>phone=new ObservableField<>();
    public ObservableField<String>pwd=new ObservableField<>();

    @Override
    protected void initData() {

    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof UserInsertEntity){
            //刷新我的fragment界面
            //我的fragment->登录->成功,关闭登录->我的fragment显示
            //我的fragment resume每次进入都刷新数据->进入登录页在返回也会刷新->逻辑简单,程序执行步骤冗余
            //登录成功获取到用户数据后在->发送刷新消息到我的fragment->拿到用户信息在刷新，逻辑复杂，无冗余操作
            //界面间通信方式->livedata,eventbus,动态广播,返回值方式启动
            finishPage();
            EventAction action = new EventAction();
            action.action = "refresh_user";
            EventBus.getDefault().post(action);
            EventAction refresh = new EventAction();
            refresh.action = "refresh_car";
            EventBus.getDefault().post(refresh);
        }else if (entity instanceof UserEntity){
            UserEntity userEntity = (UserEntity) entity;
            if (userEntity.status == 0){
                //接收完成用户数据
                //用户信息保存到本地
                UserSingle.getInstance().login(userEntity.data);
                m.insertUser(userEntity).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(this);
            }else {
                showErro(userEntity.message);
            }
        }
    }

    public void finish(){
        finishPage();
    }

    public void register(){
        startPage(RegisterActivity.class);
        finishPage();
    }

    //忘记密码
    public void forget(){
        startPage(ForgetPWDActivity.class);
//        finishPage();
    }

    //登录方法
    public void login(){
        if (TextUtils.isEmpty(phone.get())){
            showErro("手机号为空");
            return;
        }
        if (TextUtils.isEmpty(pwd.get())){
            showErro("密码为空");
            return;
        }
        Map<String,Object>map = new HashMap<>();
        map.put("mobile",phone.get());
        map.put("pushId","");
        map.put("pwd",pwd.get());
        m.requestLogin(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    //三方点击登录监听
    public void sinaLogin(){

    }

}
